package com.example.demo.controller;

import com.example.demo.dataobject.Insulin;
import com.example.demo.dataobject.MedicineShorthand;
import com.example.demo.dataobject.OralMedicine;
import com.example.demo.service.InsulinService;
import com.example.demo.service.MedicineShorthandService;
import com.example.demo.service.OralMedicineService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Timestamp;
import java.util.List;

@RestController
@RequestMapping("/medicine")
@Slf4j
@CrossOrigin(allowCredentials = "true", allowedHeaders = "*")  //解决跨域请求,但是无法做到对于session的共享，无法针对ajax请求，所以需要对参数值进行设置
public class MedicineShorthandController {


    @Autowired
    private MedicineShorthandService medicineShorthandService;

    @Autowired
    private InsulinService insulinService;

    @Autowired
    private OralMedicineService oralMedicineService;



    @RequestMapping("/modifyMedicineShorthandRecord")
    public String modifyMedicineShorthandRecord(@RequestParam("id") Integer id,
                                                @RequestParam("telephone") String telephone,
                                                @RequestParam("insulinName") String insulinName,
                                                @RequestParam("insulinDose") Integer insulinDose,
                                                @RequestParam("oralMedicineName") String oralMedicineName,
                                                @RequestParam("oralMedicineDose") Integer oralMedicineDose,
                                                @RequestParam("remarks") String remarks,
                                                @RequestParam("recordTime") Timestamp recordTime) {
        MedicineShorthand result = medicineShorthandService.saveModification(id, telephone, insulinName, insulinDose, oralMedicineName, oralMedicineDose, remarks, recordTime);
        if (result == null) {
            return "ERROR";
        }
        return "SUCCESS";

    }


    @RequestMapping("/listOralMedicine")
    public List<OralMedicine> listOralMedicine() {
        List<OralMedicine> result = oralMedicineService.findAll();
        return result;
    }

    @RequestMapping("/searchOralMedicine")
    public List<OralMedicine> searchOralMedicine(@RequestParam("name") String name) {
        List<OralMedicine> result = oralMedicineService.findByNameLikeOrderById("%" + name + "%");
        return result;
    }

    @RequestMapping("/listInsulin")
    public List<Insulin> listInsulin() {
        List<Insulin> result = insulinService.findAll();
        return result;
    }

    @RequestMapping("/searchInsulin")
    public List<Insulin> searchInsulin(@RequestParam("name") String name) {
        List<Insulin> result = insulinService.findByNameLikeOrderById("%" + name + "%");
        return result;
    }

    @RequestMapping("/saveMedicineShorthand")
    public String saveMedicineShorthand(@RequestParam("telephone") String telephone,
                                        @RequestParam("insulinName") String insulinName,
                                        @RequestParam("insulinDose") Integer insulinDose,
                                        @RequestParam("oralMedicineName") String oralMedicineName,
                                        @RequestParam("oralMedicineDose") Integer oralMedicineDose,
                                        @RequestParam("remarks") String remarks,
                                        @RequestParam("recordTime") Timestamp recordTime) {
        MedicineShorthand result = medicineShorthandService.save(telephone, insulinName, insulinDose, oralMedicineName, oralMedicineDose, remarks, recordTime);
        if (result == null) {
            return "ERROR";
        }
        return "SUCCESS";
    }


    @RequestMapping("/listMedicineShorthand")
    public List<MedicineShorthand> listMedicineShorthand(@RequestParam("telephone") String telephone) {
        List<MedicineShorthand> result = medicineShorthandService.findByTelephoneOrderByRecordTimeDesc(telephone);
        return result;
    }

    @RequestMapping("/deleteMedicineShorthand")
    public String deleteMedicineShorthand(@RequestParam("id") Integer id) {
        Boolean result = medicineShorthandService.delete(id);
        if (result) {
            return "SUCCESS";
        }
        return "ERROR";
    }


}
